<?php 
/**
 * 通用功能类
 */
class Common
{
	/**
	 * 解除转义
	 * @param	mixed $var
	 * @return	mixed
	 */
	public static function prepareGPCData( & $var )
	{
		if( is_array( $var ) )
		{
			while( ( list( $key , $val ) = each( $var ) ) != null )
			{
				$var[$key] = self::prepareGPCData( $val );
			}
		}
		else 
		{
			$var = stripslashes( $var );
		}
		
		return $var;
	}
	
	/**
	 * 获取系统配置信息
	 * @param	string $key		配置文件项
	 * @return	array
	 */
	public static function & getConfig( $key = '' )
	{
		static $config = array();
		if( !$config )
		{
			$config = self::getConfigFile( 'SystemConfig' );
			
			$config = array_merge_recursive( $config , self::getConfigFile( "GameConfig" ) );
		}

		if( !isset( $config[$key] ) )
		{
			$config[$key] = self::getConfigFile( $key );
		}
		
		if( $key )		
			return $config[$key];
		
		return $config;
	}
	
	/**
	 * 设置系统配置
	 * @param	string $key		配置文件项
	 * @param	mixed $value	配置值
	 */
	public static function setConfig( $key , $value )
	{
		$content = "<?php\n";
		$content .= "return ". var_export( $value , true ) .";\n";
		$content .= "?>";
		$fp = fopen( CONFIG_DIR . "/{$key}.php" , "w" );
		fwrite( $fp , $content );
		fclose( $fp );
	}
	
	/**
	 * 获取Cache实例
	 * @param	string $param	Cache服务器名称
	 * @return	iCache
	 */
	public static function & getCache( $param = 'data' )
	{
		static $cache = array();
		if( empty( $cache[$param] ) )
		{
			$config = self::getConfig( 'memcache' );
			if( empty( $config[$param] ) )
			{
				$cache[$param] = false;
				
				//系统错误,缺少必要的Memcache配置
				return false;
			}
			else
			{
				$memcacheClass = self::getConfig( 'memcacheClass' );
				$cache[$param] = new $memcacheClass( $config[$param] );
			}
		}
		
		return $cache[$param];
	}
	
	/**
	 * 获取DB实例
	 * @param	string $dbName	DB名称
	 * @return	iDber
	 */
	public static function & getDB( $uId = 0 )
	{
		$dbClassName = Common::getConfig( 'dbClassName' );
		switch ( $dbClassName )
		{
			case 'MysqlDber':
				return MysqlDber::getInstance( $uId );
			case 'Dber':
				return Dber::getInstance( $uId );
		}
		throw new GameException( 101 );
	}
	
	/**
	 * 获取框架客户端
	 * @param	string $serverName	服务器名称
	 * @return	RestClient
	 */
	public static function & getClient( $serverName = 'frameServer' )
	{
		static $client = array();
		if( empty( $client[$serverName] ) )
		{
			$config = Common::getConfig( $serverName );
			$client[$serverName] = new RestClient( $config['url'] , $config['protocalType'] , $config['timeout'] );
		}
		return $client[$serverName];
	}

	/**
	 * 获取Task对象
	 * @param	int $uId			用户ID
	 * @return	FM_TaskSystem
	 */
	public static function & getTask( $uId )
	{
		return FM_TaskSystem::getInstance( $uId );
	}

	/**
	 * 获取统计器对象
	 * @param	int $uId			用户ID
	 * @return	FM_Accumulator
	 */
	public static function & getAccumulator( $uId )
	{
		return FM_Accumulator::getInstance( $uId );
	}
	
	/**
	 * 计算最小的不重复值
	 * @param	array $ids			数字
	 * @param	int $min			最小值
	 * @return	int
	 */
	public static function computeMinUnique( $ids , $min = 1 )
	{
		array_multisort( $ids , SORT_ASC );
		foreach ( $ids as $item )
		{
			if( $min == $item )
			{
				$min++;
			}
		}
		return $min;
	}
	
	/**
	 * 根据概率计算结果
	 * @param	int $probability( 0 ~ 100 支持两位小数)
	 * @return	boolean
	 */
	public static function computeResult( $probability )
	{
		$seed = rand( 1 , 10000 );
		if( $seed <= $probability * 100 )
			return true;
		return false;
	}
	
	/**
	 * 获取配置文件内容
	 * @param string $configKey	配置文件索引键
	 */
	protected static function & getConfigFile( $configKey )
	{
		$config = array();
		if( file_exists( CONFIG_DIR . "/{$configKey}.php" ) )
		{
			$config = require( CONFIG_DIR . "/{$configKey}.php" );
		}
		
		return $config;
	}
	
	/**
	 * 注册自动加载类文件方法
	 */
	public static function registerAutoLoad()
	{
		spl_autoload_register( array( 'Common' , 'autoLoad' ) );
	}
	
	/**
	 * 自动加载类文件
	 * @param string $classname
	 */
	public static function autoLoad( $className )
	{
		$className = str_replace( '_' , '/' , $className );
		
		//在模块文件夹搜索
		if( file_exists( MOD_DIR . "/{$className}.php" ) )
		{
			include_once( MOD_DIR . "/{$className}.php" );
			return ;
		}
		
		//在控制器文件夹搜索
		if( file_exists( CON_DIR . "/{$className}.php" ) )
		{
			include_once( CON_DIR . "/{$className}.php" );
			return ;
		}
	}
}